package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.ArticleMapper;
import com.example.forum.dao.BoardMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.Board;
import com.example.forum.model.User;
import com.example.forum.services.IArticleService;
import com.example.forum.services.IBoradService;
import com.example.forum.services.IUserService;
import com.example.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.graphql.GraphQlProperties;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private IUserService iUserService;


    @Autowired
    private IBoradService boradService;
    @Override
    public void create(Article article) {
        if(article==null
        || StringUtil.isEmpty(article.getContent())
        ||StringUtil.isEmpty(article.getTitle())
        ){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed("插入数据中存在空值"));
        }
        article.setVisitCount(0);
        article.setReplyCount(0);
        article.setLikeCount(0);
        article.setDeleteState((byte)0);
        article.setState((byte)0);
        Date date = new Date();
        article.setUpdateTime(date);
        article.setCreateTime(date);
        int row = articleMapper.insertSelective(article);
        if(row<=0){
            log.warn("插入数据失败");
            throw new ApplicationException(AppResult.failed("插入数据失败"));
        }

        //插入数据成功 获取用户信息，跟新用户发帖数
        User user = iUserService.SelectById(article.getUserId());
        iUserService.addOnceArticleCountById(user.getId());

        //获取板块信息，跟新板块发帖数
        Board board = boradService.selectById(article.getBoardId());
        boradService.addOnceArticleCountById(board.getId());

        log.info(ResultCode.SUCCESS.toString() + "user id : " + article.getUserId()
        + "board id :" + article.getBoardId() + "article id:" + article.getId());


    }

    @Override
    public List<Article> selectAll() {
        List<Article> articles = articleMapper.selectAll();
        return articles;
    }

    @Override
    public List<Article> selectByBoardId(Long boardId) {
        //校验是否为空
        if(boardId==null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed("校验失败"));
        }
        List<Article> articles = articleMapper.selectByBoardId(boardId);
        return articles;
    }

    @Override
    public Article selectDetailById(Long id) {
        if(id==null||id<=0){
            log.warn("传入的id不正确");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectDetailById(id);
        if(article==null){
            log.warn("查询的结果为null");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }

        //构造跟新对象，跟新访问次数
        Article update = new Article();
        update.setId(article.getId());
        update.setVisitCount(article.getVisitCount()+1);
        int i = articleMapper.updateByPrimaryKeySelective(update);
        if(i!=1){
            throw new ApplicationException(AppResult.failed("跟新访问次数时出现异常"));
        }
        article.setVisitCount(article.getVisitCount()+1);
        return article;
    }

    @Override
    public void modify(Long id, String title, String content) {
        if(id==null||id<=0||StringUtil.isEmpty(title)||StringUtil.isEmpty(content)){
            log.warn("传入的参数出错");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = new Article();
        article.setId(id);
        article.setTitle(title);
        article.setContent(content);
        article.setUpdateTime(new Date());
        int i = articleMapper.updateByPrimaryKeySelective(article);
        if(i!=1){
            log.info("跟新失败");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

    }

    @Override
    public Article selectById(Long id) {
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article==null){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        return article;
    }

    //点赞
    @Override
    public void thumbsUpById(Long id) {
        if(id==null||id<=0){
            log.info("id出现错误");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));

        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article==null){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        //构造跟新对象
        Article update = new Article();
        update.setId(article.getId());
        update.setLikeCount(article.getLikeCount()+1);
        update.setUpdateTime(new Date());

        //跟新数据库
        int i = articleMapper.updateByPrimaryKeySelective(update);
        if(i!=1){
            log.info("点赞跟新失败");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));

        }


    }

    @Override
    public void deleteById(Long id) {
        if(id==null||id<=0){
            log.warn("传入的id不正确");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectDetailById(id);
        if(article==null||article.getDeleteState()==1){
            log.warn("查询的结果为null");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        Article update = new Article();
        update.setId(article.getId());
        update.setDeleteState((byte) 1);
        int i = articleMapper.updateByPrimaryKeySelective(update);
        if(i!=1){
            // 打印⽇志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //跟新板块中帖子的数量
        boradService.subOneArticleCountById(article.getBoardId());
        //跟新用户发帖数量
        iUserService.subOneArticleCountById(article.getUserId());


    }

    @Override
    public void addOnceReplayArticleCountById(Long id) {    //跟新回复帖子的数量
        if(id==null||id<=0){
            log.warn("传入的id不正确");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article==null||article.getDeleteState()==1||article.getState()==1){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));

        }
        Article update = new Article();
        update.setId(article.getId());
        update.setReplyCount(article.getReplyCount()+1);
        update.setUpdateTime(new Date());

        int i = articleMapper.updateByPrimaryKeySelective(update);
        if(i!=1){
            log.info("跟新失败");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public List<Article> selectByUserId(Long userid) {
        if(userid==null||userid<=0){
            log.warn("传入的id不正确");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Article> result = articleMapper.selectByUserId(userid);
        return result;

    }
}
